package primalgorithms.array;

public class _11RotateArray {
    public static void rotate(int[][] matrix) {
        /**
         * 矩阵的旋转实际上是经历了两次对称变换：
         * 1.上下对称变换
         * 2.对角线对称变换
        */
        if(matrix == null || matrix.length == 1){
            return;
        }
        /**
         * 注意二维数组的length
         * 行数： arr.length
         * 列数： arr[0].length
         */
        int rowLen = matrix.length;
        int colLen = matrix[0].length;
        //上下交换
        //注意此处行下标的上限
        for (int i = 0;i < rowLen/2;i++){
            int[] temp = matrix[i];
            matrix[i] = matrix[rowLen - i - 1];
            matrix[rowLen - i -1] = temp;
        }
        //对角线交换
        for (int i = 0;i < rowLen;i++){
            for (int j = i+1;j < colLen;j++){
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }

    }
}
